package com.lzx.adx.report.support.strategy.v3.channel;

import cn.hutool.http.HttpUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.OcpxAccountDTO;
import com.lzx.adx.report.dto.ProductConfigDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.support.strategy.v2.channel.AbstractReport;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
@ProductType(ProductChannelConstant.QI_SHI_ZHE)
public class QiShiZheReport extends AbstractProductReport {
    @Override
    public String execute(ReportDTO reportDTO, OcpxAccountDTO ocpxAccountDTO, ProductConfigDTO productConfigDTO) {
        Map<String,Object>  params=new HashMap<>();
        params.put("platCampaignId", reportDTO.getCampaignId());
        params.put("adgroup_id", reportDTO.getAdgroupId());


        params.put("creattveId", reportDTO.getContentId());

        if(StringUtils.isNotBlank(reportDTO.getIp()) && !StringUtils.contains(reportDTO.getIp(), "__")){
            params.put("ip", reportDTO.getIp());
        }
        if(StringUtils.isNotBlank(reportDTO.getUser_agent()) && !StringUtils.contains(reportDTO.getUser_agent(), "__")){
            String ua = URLEncoderDecoderTool.decodeURL(reportDTO.getUser_agent());
            params.put("ua", URLEncoderDecoderTool.encodeURL(ua));
        }
        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) &&!StringUtils.contains(reportDTO.getImeiSum(),"__")){
            params.put("imeiMd5", reportDTO.getImeiSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaid()) &&!StringUtils.contains(reportDTO.getOaid(),"__")){
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) &&!StringUtils.contains(reportDTO.getOaidSum(),"__")){
            params.put("oaidMd5", reportDTO.getOaidSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfa()) &&!StringUtils.contains(reportDTO.getIdfa(),"__")){
            params.put("idfa", reportDTO.getIdfa());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfaSum()) &&!StringUtils.contains(reportDTO.getIdfaSum(),"__")){
            params.put("idfaMd5", reportDTO.getIdfaSum());
        }
        String allCabackUrl = String.format(productConfigDTO.getCallbackUrl(), reportDTO.getBizRequestId());
        params.put("callback", allCabackUrl);
        String urlParams = UrlUtil.covertParamsOrderByAsc(params);

        String finalUrl = ocpxAccountDTO.getReportUrl() + "&" + urlParams;
        log.info("账号为{},请求产品端的完整地址为 {}", ocpxAccountDTO.getId(), finalUrl);
        String body = HttpUtil.createGet(finalUrl).execute().body();
        log.info("账号为{} 请求产品端，响应的结果为{}", ocpxAccountDTO.getId(), body);
        ocpxAccountDTO.setFinalURL(finalUrl);
        return body;
    }
}
